//
//  SpringAnimationVC.m
//  Home
//
//  Created by 曹连华 on 2019/7/2.
//  Copyright © 2019 曹连华. All rights reserved.
//

#import "SpringAnimationVC.h"
#import "SpringAnimationViewModel.h"
#import "SpringAnimationDelegate.h"
#import <Masonry/Masonry.h>
#import <ServiceCode/ServiceCode.h>
#import <CoreCode/UIColor+Convenience.h>

@interface SpringAnimationVC ()<UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SpringAnimationViewModel *viewModel;

@property (nonatomic, strong) UIView *animationView;
@property (nonatomic, strong) UIView *animationContentView;
@property (nonatomic, strong) UIButton *groupAnimation;

@end

@implementation SpringAnimationVC

+ (void)load {
    RouterVO *vo = [RouterVO new];
    vo.tab = NO;
    vo.createdType = PBRouteObjectCreateByCode;
    vo.className = NSStringFromClass(self);
    vo.keyInfo = @"弹性动画示例页面";
    [[Router sharedInstance]registerRouterVO:vo withKey:@"springanimation"];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self.viewModel.dataSource;
        _tableView.estimatedRowHeight = 120;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tableView;
}
- (UIView *)animationView {
    if (!_animationView) {
        _animationView = [[UIView alloc]init];
        _animationView.backgroundColor = [UIColor blueColor];
        _animationView.layer.shadowOffset = CGSizeMake(0, -10);
        _animationView.layer.shadowColor = [UIColor redColor].CGColor;
        _animationView.layer.shadowOpacity = 1.f;
        _animationView.layer.shadowRadius = 5.f;
        _animationView.layer.cornerRadius = 15.f;
        
    }
    return _animationView;
}
- (UIView *)animationContentView {
    if(!_animationContentView) {
        _animationContentView = [[UIView alloc]init];
        _animationContentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _animationContentView;
}
- (UIButton *)groupAnimation {
    if (!_groupAnimation) {
        _groupAnimation = [[UIButton alloc]init];
        _groupAnimation.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_groupAnimation setTitle:@"组合动画" forState:UIControlStateNormal];
        [_groupAnimation setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _groupAnimation.titleLabel.font = [UIFont systemFontOfSize:13];
        [_groupAnimation addTarget:self action:@selector(groupAnimationAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _groupAnimation;
}
- (SpringAnimationViewModel *)viewModel {
    if(!_viewModel) {
        _viewModel = [[SpringAnimationViewModel alloc]init];
    }
    return _viewModel;
}
-(void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

- (void)initUI {
    self.navigationItem.title = @"CASpringAnimation";
    
    [self setNaviButtonText:@"恢复原样" textColor:hex(0X2323232) position:BarButtonItemPositionRight];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.animationContentView];
    [self.animationContentView addSubview:self.animationView];
    [self.view addSubview:self.groupAnimation];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.leading.mas_equalTo(0);
        make.width.mas_equalTo(180);
    }];
    
    [self.animationContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(self.tableView.mas_right).mas_equalTo(0);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(self.groupAnimation.mas_top).mas_equalTo(-10);
    }];
    
    [self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    [self.groupAnimation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-30);
        make.bottom.mas_equalTo(-30);
        make.height.mas_equalTo(40);
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self rightBtnClicked:nil];
    SpringAnimationTVCellModel *rowModel = (SpringAnimationTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:indexPath];
    CABasicAnimation *animation = [self getAnimationKeyPath:rowModel.subTitle fromValue:rowModel.formValue toValue:rowModel.toValue];
    [self.animationView.layer addAnimation:animation forKey:rowModel.subTitle];
    
}
- (CASpringAnimation *)getAnimationKeyPath:(NSString *)keyPath fromValue:(id)fromValue toValue:(id)toValue{
    if (@available(iOS 9.0, *)) {
        CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:keyPath];
        springAnimation.fromValue = fromValue;
        /*byvalue是在fromvalue的值的基础上增加量*/
        //basicAnimation.byValue = @1;
        springAnimation.toValue = toValue;
        //initialVelocity 初始速率，动画视图的初始速度大小。默认值：0 速度方向与运动方向一致，速率为负数时，速度方向与运动方向相反；
        springAnimation.initialVelocity = 0;
        
        //damping阻尼系数，阻止弹簧伸缩的系数，阻尼系数越大，停止越快。默认值：10；
        springAnimation.damping = 2;
        
        //stiffness刚度系数(劲度系数/弹性系数)，刚度系数越大，形变产生的力就越大，运动越快。默认值： 100 ；
        springAnimation.stiffness = 10;
        
        //mass模拟的是质量，影响图层运动时的弹簧惯性，质量越大，弹簧拉伸和压缩的幅度越大 默认值：1 ；
        springAnimation.mass = 1;
        
        springAnimation.duration = springAnimation.settlingDuration;
        springAnimation.repeatCount = CGFLOAT_MAX;
        
        //由于CAAnimation的delegate使用的strong类型  声明一个单独的类实现delegate
        springAnimation.delegate = [SpringAnimationDelegate new];
        springAnimation.removedOnCompletion = NO;
        
        /*
         * kCAFillModeRemoved : 动画结束后,layer会恢复到之前的状态
         * kCAFillModeForwards : 动画结束后,layer会一直保持着动画最后的状态
         * kCAFillModeBackwards: 这个和kCAFillModeForwards是相对的,就是在动画开始前,你只要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始.你可以这样设定测试代码,将一个动画加入一个layer的时候延迟5秒执行.然后就会发现在动画没有开始的时候,只要动画被加入了layer,layer便处于动画初始状态
         * kCAFillModeBoth: 动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态.
         */
        springAnimation.fillMode = kCAFillModeForwards;
        return springAnimation;
    } else {
        // Fallback on earlier versions
        return nil;
    }
    
}
- (void)groupAnimationAction:(UIButton *)btn {
    [self.animationView.layer removeAllAnimations];
    
}

- (void)rightBtnClicked:(id)sender {
    [self.animationView.layer removeAllAnimations];
}

- (void)vc_reload_TableView {
    [self.tableView reloadData];
}
@end
